//
//  ViewController.m
//  UI7_通讯录归档反归档练习
//
//  Created by Hanlingyu on 16/8/6.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "ViewController.h"
#import "Student.h"
#import "AddStudentViewController.h"


@interface ViewController ()
<
    UITableViewDelegate,
    UITableViewDataSource,
    NSCoding,
    AddStudentDelegate
>

@property (nonatomic, strong) UITableView *studentTableView;
@property (nonatomic, strong) NSMutableArray *studentArray;

@end

@implementation ViewController

- (void)createEditButton {
    
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addStudent)];
    
}
- (void)addStudent {
    
    AddStudentViewController *addStuVC = [[AddStudentViewController alloc] init];
    [self presentViewController: addStuVC animated:YES completion:^{
        
        
    }];
    addStuVC.delegate = self;
    
    
    
}

// 添加联系人的代理方法
- (void)addStudentToArray:(Student *)stu {
    
    NSString *sandPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *fielPath = [sandPath stringByAppendingPathComponent:@"DLS160710.plist"];
    NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:fielPath];
    [_studentArray addObject: stu];
    [array isEqualToArray:_studentArray];
    [NSKeyedArchiver archiveRootObject:array toFile:fielPath];
    [_studentTableView reloadData];

    
}

- (void)viewWillAppear:(BOOL)animated {
    

    
}

// 数据处理
- (NSMutableArray *)handleDate {
    
    
    
    NSString *sandBox = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES) lastObject];
    NSString *docPath = [sandBox stringByAppendingPathComponent:@"DLS160710.plist"];
    NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    
    
    if (array == nil) {
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160710" ofType:@"plist"];
        NSArray *pathArray = [NSArray arrayWithContentsOfFile:path];
        
        NSMutableArray *arr = [NSMutableArray array];
        for (NSDictionary *objc in pathArray) {
            Student *stu = [[Student alloc] init];
            [stu setValuesForKeysWithDictionary:objc];
            [arr addObject:stu];
        }
        [NSKeyedArchiver archiveRootObject:arr toFile:docPath];
        NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES) lastObject];
        NSString *filepath = [sandBoxPath stringByAppendingPathComponent:@"DLS160710.plist"];
        NSMutableArray *fileArr = [NSKeyedUnarchiver unarchiveObjectWithFile:filepath];
        return fileArr;
        
    }
    
    return array;
    
}

- (void)createTableView {
    
    self.studentTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    _studentTableView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_studentTableView];
    
    _studentTableView.dataSource = self;
    _studentTableView.delegate = self;
    
    [_studentTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.studentArray = [self handleDate];
    [self createEditButton];
    [self createTableView];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _studentArray.count;
    
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewCellEditingStyleDelete;
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    Student *stu = _studentArray[indexPath.row];
    cell.textLabel.text = stu.name;
    return cell;
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    
    [super setEditing:editing animated:animated];
    [_studentTableView setEditing:editing animated:animated];
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        
        NSString *sandBox = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES) lastObject];
        NSString *docPath = [sandBox stringByAppendingPathComponent:@"DLS160710.plist"];
        NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
        Student *student = _studentArray[indexPath.row];
        for (Student *stu in _studentArray) {
            if ([stu.name isEqualToString:student.name]) {
                [array removeObject:stu];
            }
            break;
        }
        [NSKeyedArchiver archiveRootObject:array toFile:docPath];
        [_studentArray removeObjectAtIndex:indexPath.row];
        [_studentTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        
        
    }
    
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
